//-----------------------------------------------------------------------------
//  Copyright (C) 2005-2008 Thomas S. Ullrich 
//
//  This file is part of the Solitaire Mahjong project.
//
//  This file may be used under the terms of the GNU General Public License.
//  This project is free software; you can redistribute it and/or modify it
//  under the terms of the GNU General Public License.
//  
//  Author: Thomas S. Ullrich
//  Last update: June 1, 2007
//-----------------------------------------------------------------------------
#include "smAboutDialog.h"
#include "smVersion.h"
#include "smDefinitions.h"
#include <QVariant>
#include <QFrame>
#include <QPushButton>
#include <QLabel>
#include <QLayout>
#include <QImage>
#include <QPixmap>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPainter>
#include <QtDebug>
#include <iostream>

using namespace std;

smAboutDialog::smAboutDialog(QWidget* parent, Qt::WindowFlags fl)
    : QDialog(parent, fl),
      mTitlePixmap(":/aboutImage/images/aboutPixmap.png"),
      mVersionText(QString("Version ") + QString(VERSION))
{
	setModal(true);

    //
	//  Merge version string into pixmap
	//
    QPainter p; 
    int strWidth = p.fontMetrics().width( mVersionText ); 
    int strHeight = p.fontMetrics().height(); 
    p.begin( &mTitlePixmap ); 
    p.drawText(QRect(10, 7, strWidth, strHeight), 0, mVersionText ); 
    p.end(); 
	
    //
    //  First define the pixmap label
    //
    int w = mTitlePixmap.width();
    int h = mTitlePixmap.height();
    
    mTitlePixmapLabel = new QLabel(this);

    mTitlePixmapLabel->setGeometry(QRect( 0, 0, w, h));
    mTitlePixmapLabel->setFixedSize(QSize(w, h));
    mTitlePixmapLabel->setLineWidth(0);
	mTitlePixmapLabel->setPixmap(mTitlePixmap);
    mTitleShown = true;

    mTitlePixmapLabel->setScaledContents(false);
    mTitlePixmapLabel->setAlignment(Qt::AlignLeft|Qt::AlignTop);
    mTitlePixmapLabel->setIndent(10); // for text

    //
    //  Now the line (just looks nicer with it)
    //
    mLine = new QFrame(this);
    mLine->setGeometry(QRect(0, h, w, 5));
    mLine->setFrameShape(QFrame::HLine);
    mLine->setFrameShadow(QFrame::Sunken);

    //
    //  The buttons
    //
    mButtonOk = new QPushButton(this);
    mButtonOk->setAutoDefault(true);
    mButtonOk->setDefault(true);
    mButtonOk->setText( tr("&Ok" ));
    mButtonLicense = new QPushButton(this);
    mButtonLicense->setText(tr("&License"));

    //
    //  Then the dialog itself
    //
    setWindowTitle(tr( "About Solitaire Mahjong"));
    setSizeGripEnabled(false);
    setFixedWidth(w);

    //
    //  Layout the buttons
    //
    QSpacerItem* spacer = new QSpacerItem (300,20, QSizePolicy::Expanding, QSizePolicy::Minimum );
    mButtonLayout = new QHBoxLayout;
    mButtonLayout->addWidget(mButtonLicense);
    mButtonLayout->addItem(spacer);
    mButtonLayout->addWidget(mButtonOk);
    mButtonLayout->setMargin(4);

    //
    //  Overall widget layout
    //
	mLayout = new QVBoxLayout(this);
	mLayout->setSpacing(0);
	mLayout->setMargin(0);
    mLayout->addWidget(mTitlePixmapLabel);
    mLayout->addWidget(mLine);
    mLayout->addLayout(mButtonLayout);

    setFixedSize(mLayout->minimumSize());
    
    //
    //  Connect slots
    //
    connect( mButtonOk, SIGNAL(clicked()), this, SLOT(accept()));
    connect( mButtonLicense, SIGNAL(clicked()), this, SLOT(license()));
}

void smAboutDialog::accept()  // Ok button pressed
{
    //
    // Hide it and make sure the next time it pops
    // up the title page is shown.
    //
    hide();
    if (!mTitleShown) license();
}

void smAboutDialog::license()  // License button pressed
{
    if (mTitleShown) {
        QPalette palette;
        palette.setColor(mTitlePixmapLabel->backgroundRole(), Qt::white);// white background
        mTitlePixmapLabel->setPalette(palette);       
        mButtonLicense->setText(tr("Tit&le && Version"));
        mTitleShown = false;
        mTitlePixmapLabel->setText(
            "Copyright (C) 2005-2008 Thomas S. Ullrich\n"
            "\n"
            "This program is free software; you can redistribute it and/or\n"
            "modify it under the terms of the GNU General Public License as\n"
            "published by the Free Software Foundation; either version 2 of\n"
            "the License, or (at your option) any later version.\n"
            "\n"
            "This program is distributed  in the hope that it will be useful,\n"
            "but without any warranty; without even the implied warranty of\n"
            "merchantability or fitness for a particular purpose. See the GNU\n"
            "General Public License for more details.\n"
            "\n"
            "You should have received a copy of the GNU General Public\n"
            "License along with this program; if not, write to the Free \n"
            "Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,\n"
            "MA 02110-1301, USA."
            );
    }
    else {
        mTitlePixmapLabel->setPixmap(mTitlePixmap);	
        mButtonLicense->setText(tr("&License"));
        mTitleShown = true;
    }
    update();
}
